﻿package view
{
	import controller.GameController;
	
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	import model.Product;
	
	public class ProductWrapper extends Sprite
	{
		private var product:Product;
		private var posX:uint;
		private var posY:uint;
		private var loadPic:Loader;
		private var tween1:Tween;
		private var tween2:Tween;
		private var tween3:Tween;		
		private var tween4:Tween;
		private var tween5:Tween;	
		private var tween6:Tween;
		private var tween7:Tween;
		private var tweenShelf:Tween;
		private var tweenAlpha:Tween;
		private var timer:Timer = new Timer(1500, 1);
		private var gameControl:GameController;
		private var productWrapper:ProductWrapper;
		private var paramDictTween:Dictionary = new Dictionary();
		private var paramDictTimer:Dictionary = new Dictionary();
		private var paramDict:Dictionary = new Dictionary();
		private var scannerSound:Sound = new Sound;
		
		private var currentSoundChannel:SoundChannel;
		
		
		public function ProductWrapper(product:Product, posX:uint, posY:uint):void
		{
			this.product = product;			
			this.posX = posX;
			this.posY = posY;
			this.loadPic = new Loader;
			this.loadPic.contentLoaderInfo.addEventListener(Event.COMPLETE, doInitialScaling);
			this.loadPic.load(new URLRequest(product.getPic()));
			
			this.scannerSound.load(new URLRequest(Config.OtherSoundPath + "scanner.mp3"));
			
			//trace(this, "construct pos x, y:", this.posX, this.posY, this.product.getPic());						
		}
		
		//look for longest site, calculate scaling and scale pic
		private function doInitialScaling(event:Event):void
		{		
			//trace("vor scaling backup:", this.posX, this.posY);
			//trace("vor scaling:", this.x, this.y);			
			//fix dislocation of some pictures in shelf
			this.x = this.posX;
			this.y = this.posY;	

			this.addChild(this.loadPic);

			
			var scaling:Number = 1.0;
			if (this.width <= this.height) {
				scaling = Config.ProductHeightInShelf / this.height;
			} else {
				scaling = Config.ProductWidthInShelf / this.width;
			}

			//trace("nach erstem scaling:", this.x, this.y);			

			
			this.scale(scaling);
			if (this.height > Config.ProductHeightInShelf) {
				this.scale(Config.ProductHeightInShelf / this.height);	
			}
			
			//trace("nach zweitem scaling:", this.x, this.y);			
			
			
			/// --- problem occurs
			this.doInitialPositioning();			
			//trace("breite:", this.width, "höhe:", this.height);
		}
		
		private function doInitialPositioning():void 
		{
			//trace("vor positioning:", this.x, this.y);			
			
			//push wrapper to bottom
			this.y = this.y + (Config.ProductHeightInShelf - this.height);
			//center wrapper
			this.x = this.x + (Config.ProductWidthInShelf - this.width) / 2;
			
			//trace("nach positioning breiten:", Config.ProductWidthInShelf - this.width, "höhen:", Config.ProductHeightInShelf - this.height);	
			//trace("nach positioning:", this.x, this.y);			

			
			//update backup positions
			this.posY = this.y;
			this.posX = this.x;
			
			//trace(this, "update after pos x, y:", this.posX, this.posY, this.product.getPic());			
		}
		
		public function scale(scaling:Number):void
		{
			this.width *= scaling;
			this.height *= scaling;
		}
		
		public function getX():uint{
			//trace(this, "get this.x", this.posX, this.product.getPic());			
			return this.posX;
		}
		
		public function getY():uint{
			//trace(this, "get this.y", this.posY);			
			return this.posY;
		}
		
		public function getProduct():Product
		{
			return this.product;
		}
		
		
		/**
		 * Bewegung der Produkte
		 */ 
		public function animateTo(productWrapper:ProductWrapper, track:Array, gameController:GameController):void {
			this.productWrapper = productWrapper;
			gameControl = gameController;
			tween1 = new Tween(this.productWrapper, "x", null, this.productWrapper.x, track[0][0], Config.productSpeed, true);
			tween1 = new Tween(this.productWrapper, "y", null, this.productWrapper.y, track[0][1], Config.productSpeed, true);
			paramDictTween[tween1] = track; 
			tween1.addEventListener(TweenEvent.MOTION_FINISH, productReady);
			
		}
		
		private function productReady(e:TweenEvent):void
		{
			tween1.removeEventListener(TweenEvent.MOTION_FINISH, productReady);
			var params:Object = paramDictTween[e.currentTarget];
			tween2 = new Tween (this.productWrapper, "x", null, this.productWrapper.x, params[2][0], Config.productSpeed, true);
			tween2 = new Tween (this.productWrapper, "y", null, this.productWrapper.y, params[2][1], Config.productSpeed, true);
			paramDictTween[tween2] = params;
			tween2.addEventListener(TweenEvent.MOTION_FINISH, startTimer);	
			
		} 
		
		private function startTimer(ev:TweenEvent):void
		{
			var params:Object = paramDictTween[ev.currentTarget];
			paramDictTimer[timer] = params;
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, productRun);
			//gameControl.glowRed(this.productWrapper);
			this.productWrapper.filters = [gameControl.getScannerGlow()];
			
			if (!gameControl.getGameState()){
				this.currentSoundChannel = this.scannerSound.play();
				this.currentSoundChannel.addEventListener(Event.SOUND_COMPLETE, playProductName);
			}
			timer.start();
		}
	
		private function playProductName(e:Event):void
		{
			this.scannerSound.removeEventListener(Event.SOUND_COMPLETE, playProductName);
			this.currentSoundChannel = this.productWrapper.getProduct().getNameSound().play();
		}
	
	
		private function productRun(eve:TimerEvent):void
		{	
			gameControl.stopGlow(this.productWrapper);
			var params:Object = paramDictTimer[eve.currentTarget];
			tween6 = new Tween (this.productWrapper, "x", null, this.productWrapper.x, params[1][0], Config.productSpeed, true);
			tween6 = new Tween (this.productWrapper, "y", null, this.productWrapper.y, params[1][1], Config.productSpeed, true);
			tween6.addEventListener(TweenEvent.MOTION_FINISH, fallDown);
			
			paramDictTween[tween6] = params;
		}
		
		private function fallDown(even:TweenEvent):void
		{
			var params:Object = paramDictTween[even.currentTarget];
			paramDictTween[tween3] = params;
			
			gameControl.testHitting(this.productWrapper, this);
			tween3 = new Tween (this.productWrapper, "x", null, this.productWrapper.x, params[3][0], Config.productSpeed, true);
			tween3 = new Tween (this.productWrapper, "y", null, this.productWrapper.y, params[3][1], Config.productSpeed, true);
			tween3.addEventListener(TweenEvent.MOTION_FINISH, cancelCollider);
			
		}
		
		private function cancelCollider(event:TweenEvent):void
		{
			gameControl.removeCartListener(this.productWrapper);
		}
		
		public function backToShelf(product:ProductWrapper):void
		{
			//trace(this, "before tween backup:", this.posX, this.posY, "current:", this.x, this.y, this.product.getPic());			
			tweenShelf = new Tween(product, "alpha", null, 1, 0, 0.01, true);	
			tweenShelf = new Tween(product, "x", null, product.x, product.getX(), 0.1, true);
			tweenShelf = new Tween(product, "y", null, product.y, product.getY(), 0.1, true);
			paramDict[tweenShelf] = product;
			tweenShelf.addEventListener(TweenEvent.MOTION_FINISH, setAlpha);
		}
		
		private function setAlpha(a:TweenEvent):void
		{
			//trace(this, "after tween backup:", this.posX, this.posY, "current:", this.x, this.y, this.product.getPic());			
			
			var params:Object = paramDict[a.currentTarget];
			if(!this.gameControl.getGameState())
				tweenAlpha = new Tween(params, "alpha", null, 0, 1, 1, true);
		}
		
	}
}